package com.lm.scau.dyb.activity.base;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.lm.scau.dyb.R;
import com.lm.scau.dyb.bean.PackageBean;
import com.lm.scau.dyb.inter.HttpExecuteEvent;
import com.lm.scau.dyb.utils.FlagHelper;
import com.lm.scau.dyb.utils.HttpUtils;
import com.lm.scau.dyb.utils.MD5Util;
import com.lm.scau.dyb.utils.PhoneUtil;
import com.lm.scau.dyb.utils.SharepreferenceUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

/**
 * Function:
 * auth：liming on 2016/8/15 12:36
 * mail：liming@wxchina.com
 */
public class LoginActivity extends BaseActivity implements HttpExecuteEvent {
    private TextView registerView;
    private Button loginBtn;
    private EditText accountView;
    private EditText pwdView;
    private CheckBox keepPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        String accountCach = SharepreferenceUtil.getInstance(mActivity).getString(FlagHelper.USER_NAME);
        String pwdCach = SharepreferenceUtil.getInstance(mActivity).getString(FlagHelper.USER_PWD);
        accountView.setText(accountCach.equals("-1") ? "" : accountCach);
        pwdView.setText(pwdCach.equals("-1") ? "" : accountCach);
        initAction();
    }

    private void initAction() {
        registerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(RegisterActivity.class, null);
            }
        });
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkLegalInput()) {
                    login();
                }
            }
        });
    }

    private void login() {
        String account = accountView.getText().toString();
        String pwd = MD5Util.string2MD5(pwdView.getText().toString());
        if (keepPwd.isChecked()) {
            SharepreferenceUtil.getInstance(this).putString(FlagHelper.USER_NAME, account);
            SharepreferenceUtil.getInstance(this).putString(FlagHelper.USER_PWD, pwdView.getText().toString());
        }
        PackageBean pk = new PackageBean();
        pk.method = "userLogin";
        pk.msgname = "UserAction";
        JSONObject cont = new JSONObject();
        try {
            cont.put("account", account);
            cont.put("pwd", pwd);
            pk.cont = cont;
            httpUtils.httpRequestGet(pk.toString(), true, this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private boolean checkLegalInput() {
        if (!PhoneUtil.isMobileNO(accountView.getText().toString())) {
            showToast("账号格式不正确，请直接使用手机号码登录...");
            return false;
        }
        if ("".equals(accountView.getText().toString().trim()) || "".equals(pwdView.getText().toString().trim())) {
            showToast("请输入完整...");
            return false;
        }
        if (!PhoneUtil.isPwdlegal(pwdView.getText().toString())) {
            showToast("密码格式不正确，至少6位，\n数字、字母、字符至少包含两种。...");
            return false;
        }
        return true;
    }

    private void initView() {
        registerView = (TextView) findViewById(R.id.text_view_assign);
        accountView = (EditText) findViewById(R.id.login_edit_account);
        pwdView = (EditText) findViewById(R.id.login_edit_pwd);
        loginBtn = (Button) findViewById(R.id.btn_login);
        keepPwd = (CheckBox) findViewById(R.id.keep_pwd);
    }

    @Override
    public void onResponeSuccess(String respone) {
        try {
            JSONObject cont = new JSONObject(respone).getJSONObject("cont");
            String msg = cont.getString("msg");
            if ("".equals(msg)) { //登陆成功
                startActivity(LoginProcessActivity.class,null);
            } else {
                showToast(msg);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            showToast("解析数据错误，请重试...");
        }
    }

    @Override
    public void onResponeError(String respone) {
        showToast("未知错误，请重试...");
    }
}
